package com.shouldibuythis.android.component.quesions;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shouldibuythis.android.R;
import com.shouldibuythis.android.models.Question;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eneva on 10/4/13.
 */
public class QuestionsAdapter extends ArrayAdapter<Question> {

    private Context context;
    private int rowViewResourceId;
    private List<Question> questionList = new ArrayList<Question>();

    //private DisplayImageOptions options;

    public QuestionsAdapter(Context context, int rowViewResourceId, List<Question> questionList) {
        super(context, rowViewResourceId, questionList);
        this.context = context;
        this.rowViewResourceId = rowViewResourceId;
        this.questionList = questionList;
/*
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .displayer(new RoundedBitmapDisplayer(5))
                .build();
        */
    }

    public int getCount() {
        return this.questionList.size();
    }

    public Question getItem(int index) {
        return this.questionList.get(index);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(rowViewResourceId, parent, false);

            holder = new ViewHolder();
            holder.questionUser = (TextView)row.findViewById(R.id.question_user);
            holder.questionPhotograph = (ImageView)row.findViewById(R.id.question_photograph);
            holder.questionDescription = (TextView)row.findViewById(R.id.question_description);
            holder.questionIcon = (ImageView)row.findViewById(R.id.question_icon);

            row.setTag(holder);
        }
        else{
            holder = (ViewHolder)row.getTag();
        }

        Question question = getItem(position);

        holder.questionDescription.setText(question.getDescription());
        holder.questionUser.setText(question.getUserName());

        if(question != null && question.getStatus() != null && question.getStatus().equals("Accepted")){
            holder.questionIcon.setVisibility(View.VISIBLE);
            holder.questionIcon.setImageResource(R.drawable.ic_navigation_accept);
        }

        return row;
    }

    static class ViewHolder {
        ImageView questionPhotograph;
        TextView questionUser;
        TextView questionDescription;
        ImageView questionIcon;
    }
}
